﻿<%@ WebHandler Language="C#" Class="init" %>

using System;
using System.Web;
using Newtonsoft.Json;
using System.IO;
using System.Collections.Generic;
using System.Net;
using LitJson;
using System.Text;
using Newtonsoft.Json.Linq;

public class init : IHttpHandler {


        public static string APPID ="wxaa8505367cb6c3ef";
        public static string APPSECRET = "5f093117e65cac79ffc38cb4ba189ef9";

    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "application/json";

        //context.Response.Write("https://api.7wpp.com/uploads/20201208/7f9c9caaceee40a84fad0d80dc51a74e.png");
        //  HttpContext.Current.Response.End();
        //context.Response.Write("Hello World");


        //string picName = oc_outer_trade_no + ".JPG";
        //context.Response.ContentType = "image/jpeg";
        //context.Response.AddHeader("Content-Length", content.Length.ToString());

        //如是保存二维码的话 把这两句加上即可

        // 修改 Context.Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开

        //Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(picName, System.Text.Encoding.UTF8));
            string result = HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET);

            
        JObject jo = (JObject)JsonConvert.DeserializeObject(result);
        string session_key = jo["access_token"].ToString();


            PostMoths(session_key);
      
        string pash = @"D:\";
        string filename = "test.png";
        Byte[] imgbyte = File.ReadAllBytes(pash+filename);
        context.Response.Clear();
        context.Response.ContentType = "image/*";
        context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.CacheControl = HttpCacheability.Public.ToString();
        context.Response.Cache.SetValidUntilExpires(true);
        context.Response.BinaryWrite(imgbyte);


    }



    public static string HttpGet(string Url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.Method = "GET";
        request.ContentType = "text/html;charset=UTF-8";


        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
        string retString = myStreamReader.ReadToEnd();
        myStreamReader.Close();
        myResponseStream.Close();
        return retString;
    }  


     public static void PostMoths(string access_token)
    {
        string _url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + access_token;
        string strURL = _url;
        System.Net.HttpWebRequest request;
        request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
        request.Method = "POST";
        request.ContentType = "application/json;charset=UTF-8";
        JsonData _data = new JsonData();
        _data["path"] = "pages/index?query=1";
        _data["width"] = "430";


        string _jso = _data.ToJson();
        //string paraUrlCoded = param;
        byte[] payload;
        //payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
        payload = System.Text.Encoding.UTF8.GetBytes(_jso);
        request.ContentLength = payload.Length;
        Stream writer = request.GetRequestStream();
        writer.Write(payload, 0, payload.Length);
        writer.Close();
        System.Net.HttpWebResponse response;
        response = (System.Net.HttpWebResponse)request.GetResponse();
        System.IO.Stream s;
        s = response.GetResponseStream();
        string StrDate = "";
        string strValue = "";
        byte[] tt = StreamToBytes(s);
        //将流保存在c盘test.png文件下
        System.IO.File.WriteAllBytes(@"d:\test.png", tt);
    }
    ///将数据流转为byte[]
    public static byte[] StreamToBytes(Stream stream)
    {
        List<byte> bytes = new List<byte>();
        int temp = stream.ReadByte();
        while (temp != -1)
        {
            bytes.Add((byte)temp);
            temp = stream.ReadByte();
        }
        return bytes.ToArray();
    }




    public bool IsReusable {
        get {
            return false;
        }
    }

}